const Service = require('egg').Service;

class StudentService extends Service {
  constructor(ctx) {
    super(ctx);
    this.model = ctx.model.Student;
  }

  async getStudentList(param) {
    const option = {
      where: {
        account_id: param.account_id,
        class_id: param.class_id,
      },
      attributes: {
        include: [
          [this.app.Sequelize.literal(`(SELECT COUNT(*) FROM call_history WHERE call_history.stu_id = student.stu_id  and is_absent > 0)`), 'absent_count']
        ]
      }
    };
    try {
      const { rows, count } = await this.model.findAndCountAll(option);
      return { data: { list: rows, total: count } };
    } catch (e) {
      return { error: e.message };
    }
  }

  async getStudentTotalFromClass(classId) {
    try {
      const result = await this.model.count('student', { where: { class_id: classId }});
      return { data: result };
    } catch (e) {
      return { error: e.message };
    }
  }

  async addStudent(param) {
    const row = {
      account_id: param.account_id,
      class_id: param.class_id,
      stu_name: param.stu_name,
      stu_number: param.stu_number,
      grade: param.grade,
      sex: param.sex,
      birthday: param.birthday,
      age: param.age,
      major: param.major,
      department: param.department,
      class_name: param.class_name,
      enrollment_date: param.enrollment_date,
      phone: param.phone,
      email: param.email,
      address: param.address
    }
    try {
      return await this.model.create(row);
    } catch (e) {
      return { error: e.message };
    }
  }

  async updateStudent(param) {
    const row = {
      class_id: param.class_id,
      stu_name: param.stu_name,
      stu_number: param.stu_number,
      grade: param.grade,
      sex: param.sex,
      birthday: param.birthday,
      age: param.age,
      major: param.major,
      department: param.department,
      class_name: param.class_name,
      enrollment_date: param.enrollment_date,
      phone: param.phone,
      email: param.email,
      address: param.address
    }
    const option = {
      where: {
        stu_id: param.stu_id
      }
    }
    try {
      return await this.model.update(row, option);
    } catch (e) {
      return { error: e.message };
    }
  }


  async delStudent(param) {
    const option = {
      where: {}
    };
    if(param.class_id){
      option.where.class_id = param.class_id
    }
    if (param.stu_id_list){
      option.where.stu_id = {
        [this.ctx.app.Sequelize.Op.in]: param.stu_id_list
      }
    }
    try {
      return await this.model.destroy(option);
    } catch (e) {
      return { error: e.message };
    }
  }

  async batchAddStudent(params) {
    const { account_id, class_id, stu_list } = params
    stu_list.forEach(item => {
      item.account_id = account_id
      item.class_id = class_id
    })
    try {
      return await this.model.bulkCreate(stu_list);
    } catch (e) {
      return { error: e.message };
    }
  }

  async batchExcelAddStudent(params) {
    try {
    const { account_id, stu_list } = params

    // 先创建班级
    const classList = stu_list.map(item => ({
      account_id,
      class_name: item.class_name
    }))
    const createdClasses = await this.ctx.model.Classes.bulkCreate(classList);

    // 记录班级对应的 class_id
    const classIdMap = {};
    createdClasses.forEach(item => {
      classIdMap[item.class_name] = item.class_id
    })

    // 学生添加到对应班级
    for (const item of stu_list) {
      const { class_name, stu_list } = item
      const studentList = stu_list.map(item => {
        return {
          ...item,
          account_id: account_id,
          class_id: classIdMap[class_name]
        }
      })
      await this.model.bulkCreate(studentList);
    }
    return {}
    } catch (e) {
      return { error: e.message };
    }
  }
}

module.exports = StudentService;
